package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.*;
import com.project.myBeans.MagfacultyInfoDetail;
import com.project.myBeans.ResponseBean;
import com.project.service.*;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CrypDataHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * author: WheelChen
 * data: 2017/6/23
 * function:行政人员信息操作
 */
@Controller
@RequestMapping("/magfacultyinfo")
public class MagfacultyInfoController extends BaseService {
    @Autowired
    private MagFacultyInfoService magFacultyInfoService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UniversityService universityService;
    @Autowired
    private UserService userService;
    @Autowired
    private MagFacultyService magFacultyService;
    @Autowired
    private CrypDataHelper crypDataHelper;


    @RequestMapping(value = "/findOne/{userId}", method = RequestMethod.GET)
    @ResponseBody
    /**
     *
     * @api {get} /magfacultyinfo/findOne/:userId  根据useId查找单个
     * @apiName findOne
     * @apiGroup MagFacultyInfo
     * @apiVersion 0.0.1
     * @apiDescription 按userId查找行政人员信息
     * @apiParam {int} userId 行政人员详细信息（用户表 + 行政人员信息表）
     *
     */
    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 按userId查找行政人员信息
     *
     * @param userId 行政人员的userId
     * @return
     */
    public ResponseBean findOne(@PathVariable("userId") Integer userId) {
        ResponseBean responseBean = new ResponseBean();
        JSONObject jsonObject = new JSONObject();
        User user;
        School school = null;
        Department department = null;
        University university = null;
        MagFacultyInfo magFacultyInfo;
        try {
            user = userService.findOneByUserID(userId);
            jsonObject.put("user", user);

            magFacultyInfo = magFacultyInfoService.findByUserId(userId);
            if (magFacultyInfo != null) {
                if (magFacultyInfo.getDivisionid() != null)
                    department = departmentService.findOne(magFacultyInfo.getDivisionid());
                if (magFacultyInfo.getSchoolid() != null)
                    school = schoolService.findOne(magFacultyInfo.getSchoolid());
                if (school != null) {
                    if (school.getUniversityid() != null)
                        university = universityService.findOne(school.getUniversityid());
                }
                jsonObject.put("magFacultyInfo", magFacultyInfo);
                jsonObject.put("university", university);
                jsonObject.put("school", school);
                jsonObject.put("department", department);
            }

            responseBean.setData(jsonObject);
            responseBean.setMessage("查找成功");
            responseBean.setSuccess(true);
            return responseBean;
        } catch (Exception e) {
            responseBean.setMessage("程序异常");
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/modifyOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 根据id修改行政人员相关信息（用户表 + 行政人员信息表）
     *
     * @param magFacultyInfo
     * @return
     */
    public ResponseBean modifyOne(@RequestBody MagfacultyInfoDetail magfacultyInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            User user = magfacultyInfoDetail.getUser();
            MagFacultyInfo magFacultyInfo = magfacultyInfoDetail.getMagFacultyInfo();
            if (user != null) {
                userService.updateUser(user);
                if (magFacultyInfo != null)
                    magFacultyInfoService.modifyByUserId(magFacultyInfo);
                responseBean.setSuccess(true);
                responseBean.setMessage("修改成功");
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("无信息可修改");
            }

        } catch (Exception e) {
            responseBean.setMessage("程序异常");
            responseBean.setSuccess(false);
        }
        return responseBean;
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 创建行政人员相关信息（用户表 + 行政人员信息表）
     *
     * @param magFacultyInfo
     * @return
     */
    public ResponseBean addOne(@RequestBody MagfacultyInfoDetail magfacultyInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            User user = magfacultyInfoDetail.getUser();
            MagFacultyInfo magFacultyInfo = magfacultyInfoDetail.getMagFacultyInfo();
            if (user != null) {
                if (magFacultyInfo != null) {
                    magFacultyService.addOne(magfacultyInfoDetail);
                    responseBean.setSuccess(true);
                    responseBean.setMessage("创建成功");
                } else {
                    responseBean.setSuccess(false);
                    responseBean.setMessage("缺少行政人员信息表");
                }
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("缺少用户表信息");
            }

        } catch (Exception e) {
            responseBean.setMessage("程序异常");
            responseBean.setSuccess(false);
        }
        return responseBean;
    }

    @RequestMapping(value = "/removeOneByUserId/{userId}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 根据用户id删除行政人员账号
     *
     * @param userId
     * @return
     */
    public ResponseBean removeOneByUserId(@PathVariable("userId") Integer userId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (magFacultyService.removeOneByUserId(userId)) {
                responseBean.setSuccess(true);
                responseBean.setMessage("删除成功");
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findBatchByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     *
     * @api {post} /magfacultyinfo/findBatchByCondition  [分页][条件]查找行政人员详细信息
     * @apiName findBatchByCondition
     * @apiGroup MagFacultyInfo
     * @apiVersion 0.0.1
     * @apiDescription 根据用户表、行政人员信息表筛选
     *
     * @apiParam {json} MagfacultyInfoDetail 行政人员详细信息（用户表 + 行政人员信息表）
     * @apiParamExample {json} Request-Example:
     * {
     *     user:{},
     *     magFacultyInfo:{}
     * }
     * @apiSuccess {MagfacultyInfoDetail} magfacultyInfoDetail 行政人员详细记录(用户表 + 行政人员信息表)
     * @apiSuccess {Department} department  系信息 显示
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "magfacultyInfoDetail": {
     *           user:{},
     *           magFacultyInfo:{}
     *       },
     *       "department": {}
     *     }
     */
    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 根据用户表、行政人员信息表筛选
     *
     * @param MagfacultyInfoDetail
     * @return
     */
    public ResponseBean findBatchByCondition(@RequestBody MagfacultyInfoDetail magfacultyInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            List<JSONObject> jsonObjectList = new ArrayList<>();
            magfacultyInfoDetail.setPageIndex(magfacultyInfoDetail.getPageIndex() - 1);
            List<MagfacultyInfoDetail> magfacultyInfoDetailList = magFacultyInfoService.findBatchByCondition(magfacultyInfoDetail);
            for (MagfacultyInfoDetail magfacultyInfoDetail1 : magfacultyInfoDetailList) {
                JSONObject jsonObject = new JSONObject();
                Department department;
                User user = magfacultyInfoDetail1.getUser();
                MagFacultyInfo magFacultyInfo = magfacultyInfoDetail1.getMagFacultyInfo();
                //解密用户姓名
                if (user != null) {
                    if (user.getRealname() != null)
                        user.setRealname(crypDataHelper.decRealName(user.getRealname()));
                    //过滤敏感信息
                    user.setPassword(null);
                    user.setSalt(null);
                    magfacultyInfoDetail.setUser(user);
                }
                jsonObject.put("magfacultyInfoDetail", magfacultyInfoDetail1);
                //获取系信息
                if (magFacultyInfo != null)
                    if (magFacultyInfo.getDivisionid() != null) {
                        department = departmentService.findOne(magFacultyInfo.getDivisionid());
                        jsonObject.put("department", department);
                    }
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(magfacultyInfoDetail.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
